Thread: [Help] loop problem

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    Exclamation [Help] loop problem

    hello guys i'm new how you see
    i have problem with my code
    what i need to do is this
    if i input letter 'g'

    a
    ccc
    eeeee
    ggggggg


    it need to be all letters a,c,e,g,j jumping by 2
    and my pyramid

    Code:
    #include <stdio.h>
    
    
    void main() {
        char letter;
        int row;
        int spaces;
        int i;
        int j ;
        int k ;
        printf("Please enter a letter:\n");
        scanf("%c", &letter);
        if ((letter < 'a' && letter < 'z') && (letter% 2 == 1)) {
            printf("Invalid Input\n");
        }
        else
        for (row = 'a'; row <= letter; row ++) {
            for (spaces = letter; spaces > row; spaces--) // making space between letters
                printf(" ");
        }
        for (k = 0; k < 2 * (i = 0); k++) {
            printf("%c", letter);
            }
        letter = letter + 2;
        printf("\n");
    }

    i get into infinty loop every time


    tnx for help

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Yikes. Pass.

  3. #3
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    What an absolute mess. About the only correct statements are the declarations.

    main returns an int always, never void.

    In the first if statement, if letter < 'a' it will always also be < 'z'.
    In the second for statement you set k to zero, and have a loop end condition of k < 0. This will either occur on the 128th iteration of the loop if char is signed or never if char is unsigned. You can't be sure because the C standard never mandates if a plain char is signed or unsigned, that's a detail for the compiler author to decide.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    How I can fix that what first I should do

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    > for (k = 0; k < 2 * (i = 0); k++)
    2 * 0 is always 0

    I wonder if you meant == here?
    But then again, i isn't even initialised.

    Better variable names would help.

    > (letter < 'a' && letter < 'z')
    How about
    (letter >= 'a' && letter <= 'z')
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    But how I fix my loops?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    I think the first thing you should try and write is a loop which prints
    1 a
    3 c
    5 e
    7 g


    Then use that to then print
    a
    ccc
    eeeee
    ggggggg


    Then use that to create a pyramid with the correct indentation per line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    And when I input letter 'y'

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    What about 'y'?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2016
    Posts
    5
    [Help] loop problem-show-jpg

    like that >:
    i try to make it all day and it's work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A very simple problem but enough difficult | Loop problem
    By Kevinphp7 in forum C++ Programming
    Replies: 13
    Last Post: 06-10-2015, 11:03 AM
  2. Glass Rod Problem and big loop problem!
    By TessaG in forum C++ Programming
    Replies: 20
    Last Post: 04-12-2015, 01:32 PM
  3. While loop problem
    By SimpleMind in forum C Programming
    Replies: 11
    Last Post: 07-11-2010, 06:04 AM
  4. another for loop problem
    By Rasher in forum C Programming
    Replies: 7
    Last Post: 10-15-2009, 07:52 AM
  5. do - while loop problem
    By estranged in forum C Programming
    Replies: 8
    Last Post: 12-10-2003, 07:11 PM

Tags for this Thread